package pers.hl.lib_global.model.enums

import pers.hl.lib_global.model.bean.Book

/**
 * 书籍状态 0：新发布 1：借阅中 2：已借阅(接受) 3：已归还 4：已取消 5：已拒绝 999：无意义，用于标记只修改特殊信息(二维码id)
 */
enum class BookStatus(private val status: Int) {

    NEW(0), BORROWING(1), BORROWED(2), RETURNED(3), CANCELED(4), REFUSED(5), NONE(999);

    override fun toString(): String {
        return status.toString()
    }

    companion object {

        fun parse(type: Int): BookStatus? {
            return when (type) {
                Book.STATUS_NEW -> NEW
                Book.STATUS_BORROWING -> BORROWING
                Book.STATUS_BORROWED -> BORROWED
                Book.STATUS_RETURNED -> RETURNED
                Book.STATUS_CANCEL -> CANCELED
                Book.STATUS_REFUSED -> REFUSED
                else -> NONE
            }
        }

        fun value(enumType: BookStatus?): Int {
            return when (enumType) {
                NEW -> Book.STATUS_NEW
                BORROWING -> Book.STATUS_BORROWING
                BORROWED -> Book.STATUS_BORROWED
                RETURNED -> Book.STATUS_RETURNED
                CANCELED -> Book.STATUS_CANCEL
                REFUSED -> Book.STATUS_REFUSED
                else -> Book.STATUS_NONE
            }
        }

        fun getValueStr(type: Int): String {
            return when (type) {
                Book.STATUS_NEW -> "新发布"
                Book.STATUS_BORROWING -> "借阅中"
                Book.STATUS_BORROWED -> "已借阅"
                Book.STATUS_RETURNED -> "已归还"
                Book.STATUS_CANCEL -> "已取消"
                Book.STATUS_REFUSED -> "已拒绝"
                else -> "未知状态"
            }
        }

        fun exist(status: Int?): Boolean {
            if (status == null) {
                return false
            }
            val bookStatus = parse(status)
            return bookStatus != null
        }
    }
}